package com.silvtar.sync.ui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import com.silvtar.sync.controller.SilverySyncInit;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PairingPanel extends JPanel implements ActionListener {

    private JTextField textField;

    SilverySyncInit    ssInit;

    JButton            button_1;
    JLabel             label;

    /**
     * Create the panel.
     */
    public PairingPanel(SilverySyncInit ssInit) {
        this.ssInit = ssInit;
        setPreferredSize(new Dimension(300, 300));
        setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        add(panel, BorderLayout.NORTH);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JLabel lblip = new JLabel("对方IP");
        panel.add(lblip);

        textField = new JTextField();
        panel.add(textField);
        textField.setColumns(10);

        JPanel panel_1 = new JPanel();
        add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        button_1 = new JButton("配对");
        button_1.addActionListener(this);
        panel_1.add(button_1);

        JPanel panel_2 = new JPanel();
        add(panel_2, BorderLayout.CENTER);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[] { 130, 65, 76, 0 };
        gbl_panel_2.rowHeights = new int[] { 49, 0, 0, 0, 0 };
        gbl_panel_2.columnWeights = new double[] { 1.0, 0.0, 0.0,
                Double.MIN_VALUE };
        gbl_panel_2.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0,
                Double.MIN_VALUE };
        panel_2.setLayout(gbl_panel_2);

        JPanel panel_3 = new JPanel();
        GridBagConstraints gbc_panel_3 = new GridBagConstraints();
        gbc_panel_3.gridheight = 4;
        gbc_panel_3.gridwidth = 3;
        gbc_panel_3.insets = new Insets(0, 0, 5, 0);
        gbc_panel_3.fill = GridBagConstraints.HORIZONTAL;
        gbc_panel_3.gridx = 0;
        gbc_panel_3.gridy = 0;
        panel_2.add(panel_3, gbc_panel_3);
        panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JLabel lblNewLabel = new JLabel("配对状态：");
        panel_3.add(lblNewLabel);

        label = new JLabel("未验证");
        panel_3.add(label);

        flashState();

    }

    public void flashState() {
        if (ssInit.isValidated()) {
            label.setText("已验证");
        } else {
            label.setText("未验证");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(button_1)) {
            ssInit.startValidate();
        }
        flashState();
    }
}
